<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import { statisticsResourceAnalysis } from '../../api/homePage/home'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
      barChartData:''
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.getResourceAnalysis();
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
        grid:{
          bottom:'15%'
        },
        title: {
          text: '资源分析',
          subtext: '数量',
          textStyle:{
            fontSize: 12,
          },
          left: -5
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['Total', 'Used'],
          left: 90
        },
        calculable: true,
        xAxis: [
          {
            type: 'category',
            data: ['登机口', '提取转盘', '托盘']
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: 'Total',
            type: 'bar',
            itemStyle:{
              color: '#FFC125',
            },
            data: [
              Number(this.barChartData.djkTotalNum),
              Number(this.barChartData.tqzpTotalNum),
              Number(this.barChartData.tpTotalNum)
            ]
          },
          {
            name: 'Used',
            type: 'bar',
            data: [
              Number(this.barChartData.djkUsedNum),
              Number(this.barChartData.tqzpUsedNum),
              Number(this.barChartData.tpUsedNum)
            ]
          }
        ]
      })
    },

    //统计资源分析
    getResourceAnalysis(){
      statisticsResourceAnalysis().then(response => {
        this.barChartData = response.data;
        this.initChart();
      })
    },

  }
}
</script>
